<?php
/************************************
*   熊店
*   @file:/server/bearshop/__include/data_model/mod_user.php
*   @intro:
*   @author:new_shop
*   @email:new_shop@163.com    
*   @date:Fri Feb 12 10:27:58 CST 2010
************************************/

class mod_member extends data_model 
{
    var $common_fields = array('id','username','level','name','company','mobile','fax','email','intro','stat','cnt_order','cnt_buy');
    
    public function __construct($db_config)
    {
        parent::__construct($db_config);
        $this->_table = 'tl_member';
    }
    /**
     * 登录验证
     *
     * @param string $username
     * @param string $passwd
     * @param array $info
     * @return bool
     */
    public function login($username , $password)
    {
        $info = array();
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('username="'.$username.'" AND password="'.md5($password).'"');
        
        $aField = array('id','level' , 'name' , 'company' , 'mobile' , 'email' , 'stat');
        $rs = $this->fetch($aField);
        if(!$rs)
            return false;
            
        $this->_data['uid'] = $this->get('id');
        unset($this->_data['id']);
        return true;
    }
    /**
     * 更新登录时间
     *
     * @param int $uid
     * @return bool
     */
    public function update_logintime($uid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set('last_login' , time());
        $this->set('last_login_ip' , K::get_ip());
        return $this->update();
    }
    
    /**
     * 创建用户记录
     *
     * @param int $uid
     * @param array $info   //目前不用
     * @return bool
     */
    public function create_user($data , &$uid)
    {
        $data['_meta_password'] = $data['password'];
        $data['password'] = md5($data['password']);
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_array($data);
        $this->set('ctime' , time());
        $rs = $this->insert();
        if(!$rs)
            return false;
            
        $uid = $this->get_insert_id();
        return true;
    }
    /**
     * 创建用户记录
     *
     * @param int $uid
     * @param array $info   //目前不用
     * @return bool
     */
    public function edit_user($data , $uid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set_array($data);
        return $this->update();
    }
    /**
     * 用户列表
     *
     * @param int $stat
     * @param int $pagesize
     * @param int $page
     * @return bool
     */
    public function list_member($stat = '' , $pagesize , $page)
    {
        $this->start(K5_DATAMOD_NOHASH);
        if(is_numeric($stat))
            $this->set_condition('stat='.$stat);
            
        $start = $this->get_limit_start($page , $pagesize);
        return $this->fetch_multi('','id DESC' , $start , $pagesize);
    }
    /**
     * 编辑单值
     *
     * @param int $uid
     * @param string $field
     * @param mix $value
     * @return bool
     */
    public function edit_value($uid , $field , $value)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set($field , $value);
        return $this->update();
    }
    /**
     * 取单个用户信息
     *
     * @param int $uid
     * @return bool
     */
    public function get_user($uid , $all_info = false)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $fields = $all_info ? '' : $this->common_fields;
        $rs = $this->fetch($fields);
        if($rs)
        {
            $this->_data['uid'] = $uid;
            unset($this->_data['id']);
        }
        return $rs;
    }
    /**
     * 修改密码
     *
     * @param uid $uid
     * @param string $password
     * @return bool
     */
    public function edit_password($uid , $password)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set('password',md5($password));
        $this->set('_meta_password' , $password);
        return $this->update();
    }
    
    
    
    /**
     * 检查EMAIL
     *
     * @param string $email
     * @param bool $is_exists
     * @return bool
     */
    public function check_email($email , &$is_exists)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('email="'.$email.'"');
        $rs = $this->fetch_count($n);
        if(!$rs)
            return false;
        
        $is_exists = $n > 0 ? true : false;
        return true;
    }
    
    /**
     * 更新最后登录时间
     *
     * @param int $uid
     * @return bool
     */
    public function update_last_login($uid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set('login_time' , time());
        $this->set('login_ip' , K::get_ip());
        return $this->update();
    }
    
    public function list_by_uid($aUid)
    {
        $this->start();
        $this->set_condition('id in ('.implode(',' , $aUid).')');
        return $this->fetch_multi($this->common_fields);
    }
    
    public function get_nick($uid , &$nick)
    {
        $this->start();
        $this->set_condition('id='.$uid);
        $rs = $this->fetch(array('nick'));
        $nick = $rs ? $this->get('nick') : $uid;
        return true;
    }
    
    public function increase_order($uid)
    {
        $this->start();
        $this->set_condition('id='.$uid);
        $this->set_exp('cnt_order' , 'cnt_buy + 1');
        return $this->update();
    }
    
    public function increase_buy($uid)
    {
        $this->start();
        $this->set_condition('id='.$uid);
        $this->set_exp('cnt_buy' , 'cnt_buy + 1');
        return $this->update();
    }
}
?>